package com.xehy.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 *  Swagger 配置页面
 *
 * @author XES
 * @create 2021/2/24 9:20
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        // 添加请求参数，我们这里把token作为请求头部参数传入后端
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization").description("token令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameters.add(parameterBuilder.build());
        //配置swagger支持多个controller包
        Predicate<RequestHandler> packageOne = RequestHandlerSelectors.basePackage("com.xehy.controller");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(createApiInfo())
                .select()
                //为当前包路径
                .apis(packageOne)
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameters);
    }

    /**
     *
     * 访问地址（http://localhost:8001/swagger-ui.html）
     *
     * @return
     */
    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder()
                .title("教材征订")
                .description("小组成员：胡一博（小程序端）、许二帅（web端）")
                .termsOfServiceUrl("")
                .contact(new Contact("rsw", "http://localhost:8001/swagger-ui.html", "xes@163.com"))
                .version("1.0")
                .description("教材征订")
                .build();
    }
}
